package com.mason.forum.service.impl;

import com.mason.forum.common.ResultCode;
import com.mason.forum.dao.BoardMapper;
import com.mason.forum.exception.ApplicationException;
import com.mason.forum.model.Board;
import com.mason.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-06
 * Time: 15:29
 */
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {

        // 1. 参数校验
        if(num == null ||  num <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 2. 调用dao层
        List<Board> boards = boardMapper.selectByNum(num);
        // 3. 返回结果
        return boards;//结果为空也是正常数据
    }

    @Override
    public Board selectById(Long id) {
        // 1. 参数校验
        if(id == null ||  id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 2. 调用dao层
        Board board = boardMapper.selectByPrimaryKey(id);
        // 3. 返回结果
        return board;
    }

    @Override
    public void incrementArticleCountById(Long id) {

        // 1. 参数校验
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(ResultCode.FAILED_BOARD_ARTICLE_COUNT);
        }
        // 2. 调用dao层
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + " board id：" + id);
            throw new ApplicationException(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        if(board.getState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString() + " board id：" + id);
            throw new ApplicationException(ResultCode.FAILED_BOARD_BANNED);
        }
        Board updateBoard = new Board();
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        updateBoard.setId(id);
        int rows = boardMapper.updateByPrimaryKeySelective(updateBoard);//更新一个值就够了
        if(rows != 1) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(ResultCode.FAILED_BOARD_ARTICLE_COUNT);
        }
        // 更新成功
        log.info(ResultCode.SUCCESS.toString() + "更新成功: board id: "
                + id + ". ");
    }

    @Override
    public void decrementArticleCountById(Long id) {
        // 1. 参数校验
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(ResultCode.FAILED_BOARD_ARTICLE_COUNT);
        }
        // 2. 调用dao层
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + " board id：" + id);
            throw new ApplicationException(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        if(board.getState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString() + " board id：" + id);
            throw new ApplicationException(ResultCode.FAILED_BOARD_BANNED);
        }
        Board updateBoard = new Board();
        int count = board.getArticleCount() - 1;
        updateBoard.setArticleCount(Math.max(count, 0));
        updateBoard.setId(id);
        int rows = boardMapper.updateByPrimaryKeySelective(updateBoard);//更新一个值就够了
        if(rows != 1) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(ResultCode.FAILED_BOARD_ARTICLE_COUNT);
        }
        // 更新成功
        log.info(ResultCode.SUCCESS.toString() + "更新成功: board id: "
                + id + ". ");
    }
}
